/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MortgageCore;
import simframe.simulation.*;
import java.io.*;
import java.util.*;
import data.LocalData;

/**
 *
 * @author karakots
 */
public class JobMarket extends SimObject {

    public static final Swarm.SwarmAction<JobMarket> newDay = new Swarm.SwarmAction<JobMarket>() {
        public synchronized void exec(JobMarket jm) {
            jm.newDay();
        }
    };

    protected Map<String, List<Double>> usUnemploymentRates = new HashMap<String, List<Double>>();
    // kpep track of month so we can access the correct housing price data
    // assumes 30 day months
    private int monthNum;
    private int dayOfMonth;
    private int numMonthsData;
    private int dataMonth;


    public JobMarket() throws IOException {
        loadEmploymentData();
        reset();
    }

 private void loadEmploymentData() throws IOException {
        InputStream in_stream = LocalData.GetInputStream(LocalData.Input.US_UNEMPLOYMENT_DATA);
        InputStreamReader is_reader = new InputStreamReader(in_stream);
        BufferedReader reader = new BufferedReader(is_reader);
        String line;
        while ((line = reader.readLine()) != null) {
            String[] lineParse;
            lineParse = line.split(",");
            List<Double> rates = new ArrayList<Double>();
            // get the price data
            int numDataPoints = lineParse.length;
            numMonthsData = numDataPoints;
            for (int j = 1; j < numDataPoints; j++) {
                rates.add(Double.parseDouble(lineParse[j]));
            }
            usUnemploymentRates.put(lineParse[0], rates);
        }
        reader.close();
        is_reader.close();
        in_stream.close();
    }

    public double getEmploymentChange(String stateCode) {
        // we store unemoloyment change, want emloyment change
        double rate = 0 - getCurrentRate(stateCode);
        return rate;
    }

    private double getCurrentRate(String state) {
        double rate = usUnemploymentRates.get(state).get(dataMonth);
        return rate;
    }

    private void newDay() {
       dayOfMonth++;
       if (dayOfMonth >= 30) {
           monthNum++;
           dayOfMonth =1;
       }
        // we don't want to go off the end of the data
        if (monthNum > (numMonthsData-2))
            dataMonth = numMonthsData - 2;
        else
            dataMonth = monthNum;
    }

    // SimObject required methods
    public void reset() {
        monthNum = 0;
        dayOfMonth = 0;
    }

    public boolean dead() {
        return false;
    }

}
